package com.itheima.reggie_1.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie_1.common.R;
import com.itheima.reggie_1.entity.Employee;
import com.itheima.reggie_1.entity.Orders;
import com.itheima.reggie_1.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据: {}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        log.info("page = {},pageSize = {}",page,pageSize);
//        构造分页构造器
        Page pageInfo = new Page(page, pageSize);
//        构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
//        添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);
//        执行查询
        ordersService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        log.info("page = {},pageSize = {}",page,pageSize);
//        构造分页构造器
        Page pageInfo = new Page(page, pageSize);
//        构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
//        添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);
//        执行查询
        ordersService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
}
